package com.china_tanyo.androidserialport;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android_serialport_api.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SerialPortFinder ser = new SerialPortFinder();

        String[] list = ser.getAllDevicesPath();

        try {
            //内侧的Com口是ttySAC0，外侧的是ttySAC3
            File file = new File("/dev/ttySAC3");
            SerialPort comm = new SerialPort(file, 115200, 0);
            InputStream inputStream = comm.getInputStream();
            OutputStream outputStream = comm.getOutputStream();

            String str = "Hello World!\n";
            for (int i = 0; i < 10; ++i) {
                outputStream.write(str.getBytes());
            }

            byte[] buffer = new byte[1000];
            inputStream.read(buffer);

            inputStream.close();
            outputStream.close();
            comm.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
